var pages = 1;
var newLd = -1;
$(function() {


	//搜索跳转
	$("#myInput").bind("search", function() {
		var keyWord = $('#myInput').val();
		var t = encodeURI(encodeURI(keyWord)); 
   		window.location.href='v-shop-search.html?key='+t+'&business='+CD.geturl().business;	
	});

	//全部商品
	allTemplate(function(data) {
		$('#product-right-list').html(template('allTemplate', data));
		//右侧点击跳转
		clickTo();
		console.log(data, 111);

		//上拉加载
		newLd = -1;
		pages = 1;
		upmore(newLd, pages);
	});
	//	全部商品点击事件
	$('#allproduct').on('tap', function() {
		mui('#product-right-scroll').pullRefresh().refresh(true);
		mui('.mui-scroll-wrapper-top').scroll().setTranslate(0,0);
		allTemplate(function(data) {
			$('#product-right-list').html(template('allTemplate', data));
			//	右侧点击跳转
			clickTo();
			//上拉加载
			
			newLd = -1;
			pages = 1;
			upmore(newLd, pages);
		});
	});

	//顶部左侧遍历
	firstTemplate(function(data) {
		$('#product-left-list').append(template('oneTemplate', data));
		$('#user').html(template('twoTemplate', data));
		//左侧点击事件
		$('.product-left-list li').on('tap', function() {
			mui('#product-right-scroll').pullRefresh().refresh(true);
			mui('.mui-scroll-wrapper-top').scroll().setTranslate(0,0);
			if($(this).hasClass('li-cur')) return false;
			$(this).addClass('li-cur').siblings().removeClass('li-cur');
			var thisLd = $(this).attr('data-id');

			//获取id
			secondTemplate({
				goodsType: thisLd,
				pages: 1,
				userUuid: newBusiness,
			}, function(data) {
				console.log(thisLd, 101010)
				
				$('#product-right-list').html(template('threeTemplate', data));
				console.log(data, 888);
				//右侧点击跳转
				clickTo();
			});

			//上拉加载

			newLd = thisLd;
			pages = 1;
			upmore(newLd, pages);

		});

	});

	//	左侧样式切换
	$('.product-left-list p').on('tap', function() {
		if($(this).hasClass('li-cur')) return false;
		$(this).addClass('li-cur').siblings().removeClass('li-cur');
	});
});

//判断uuid是否存在
var newBusiness = CD.geturl().business;
	if(!newBusiness){
		newBusiness="";
	}
//左边及顶部数据
var firstTemplate = function(callback) {
	$.ajax({
		type: "get",
        cache : false,
        traditional: true,
        xhrFields: {
		withCredentials: true
		},
		crossDomain: true,
		url: window.address + "/goods/findBayUuid",
		data: {
			"userUuid": newBusiness,
		},
		dataType: "json",
		success: function(data) {
			console.log(data,2222)
			callback && callback(data);
		}
	});
};

//右边数据
var secondTemplate = function(params, callback) {
	$.ajax({
		type: "get",
		url: window.address + "/goods/findBayGoodsType",
		data: params,
		dataType: "json",
		success: function(data) {
			callback && callback(data);
		}
	});
};
//全部
var allTemplate = function(callback) {
	$.ajax({
		type: "get",
        cache : false,
        traditional: true,
        		xhrFields: {
		withCredentials: true
		},
		crossDomain: true,
		url: window.address + "/goods/goodsBayUuid",
		data: {
			"pages": 1,
			"userUuid": newBusiness
		},
		dataType: "json",
		success: function(data) {
			console.log(data,3333);
			callback && callback(data);
		}
	});
}

//上拉加载
var upmore = function() {
	mui.init({
		pullRefresh: {
			container: "#product-right-scroll",
			up: {
				callback: function() {
					console.log(newLd, 89898999989)
					if(newLd < 0) {
						var that = this;
						pages++;
						console.log(pages, 333);
						$.ajax({
							type: "get",
        					cache : false,
       						traditional: true,
        					xhrFields: {
								withCredentials: true
							},
							crossDomain: true,
							url: window.address + "/goods/goodsBayUuid",
							data: {
								"pages": pages,
								"userUuid": CD.geturl().business
							},
							dataType: "json",
							success: function(data) {
								$('#product-right-list').append(template('allTemplate', data));
								//	右侧点击跳转
								console.log(newLd, 222);
								console.log(data, 0210202002)

								clickTo();
								//判断数据是否完成
								if(data.goodsBayUuid.length) {
									that.endPullupToRefresh();
								} else {
									that.endPullupToRefresh(true);
								};
							}
						});
					} else {
						var that = this;
						pages++;
						$.ajax({
							type: "get",
      					 	cache : false,
      					  	traditional: true,
        					xhrFields: {
								withCredentials: true
							},
							crossDomain: true,
							url: window.address + "/goods/findBayGoodsType",
							data: {
								"goodsType": newLd,
								"pages": pages
							},
							dataType: "json",
							success: function(data) {
								$('#product-right-list').append(template('threeTemplate', data));
								console.log(data, 898989898989898)
								//	右侧点击跳转
								clickTo();
								if(data.bayGoodsType.length) {
									that.endPullupToRefresh();
								} else {
									that.endPullupToRefresh(true);
								}
							}
						});
					}
				}
			}
		}
	});
}

//点击跳转
var clickTo = function() {
	$('.product-right-list li').on('tap', function() {
		var productId = $(this).attr('data-id');
		//console.log(productId);
		window.location.href = 'v-shop-product.html?id=' + productId+'&business='+newBusiness;
	});
}